package com.ln.property_control.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ln.property_control.mapper.BorrowMapper;
import com.ln.property_control.mapper.CheckPlanMapper;
import com.ln.property_control.pojo.*;
import com.ln.property_control.service.BorrowService;
import com.ln.property_control.utils.GetId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    private BorrowMapper borrowMapper;

    //    查询资产借用的方法
    @Override
    public Map<String, Object> getBorrowVo(Integer page, Integer limit) {
        /*创建Map集合用于存放数据，及所需的页码数*/
        Map<String, Object> map = new HashMap<>();
        Page<Object> pageObj = PageHelper.startPage(page, limit);
        List<BorrowVo> propertyVoList = borrowMapper.getBorrowVo();
        map.put("data", propertyVoList);
        map.put("code", 0);
        long total = pageObj.getTotal();
        map.put("count", total);
        return map;
    }

    @Override
    public BorrowVo getFindBorrowVo(String id) {
        BorrowVo findBorrowVo = borrowMapper.getFindBorrowVo(id);
        return findBorrowVo;
    }

    @Override
    public List<Department> getDepaId(int id) {
        List<Department> depaId = borrowMapper.getDepaId(id);
        return depaId;
    }
//    借用申请
    @Override
    public int getAddBorrow(BorrowVo borrowVo,String[] property_id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdfs = new SimpleDateFormat("yyyyMMddhhmmss");
        borrowVo.setBorrow_number("JY"+sdfs.format(new Date()));
        borrowVo.setDrawing_time(sdf.format(new Date()));
        int addBorrow = borrowMapper.getAddBorrow(borrowVo);
        borrowMapper.addBorrowPro(borrowVo.getBorrow_number(),property_id);
        return addBorrow;
    }

    @Override
    public int getUpdaSta(BorrowVo borrowVo, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        borrowVo.setApprover(userInfo.getUname());
        int addBorrow = borrowMapper.getUpdaSta(borrowVo);
        return addBorrow;
    }

    @Override
    public List<PropertyVo> getProperty(String property_dep_id) {
        List<PropertyVo> property = borrowMapper.getProperty(property_dep_id);
        return property;
    }

    @Override
    public List<BorrowVo> getBorrowName(String borrow_id) {
        List<BorrowVo> borrowName = borrowMapper.getBorrowName(borrow_id);
        return borrowName;
    }


    @Override
    public Map<String,Object> getReturn(Integer page , Integer  limit) {
        /*创建Map集合用于存放数据，及所需的页码数*/
        Map<String, Object> map = new HashMap<>();
        Page<Object> pageObj = PageHelper.startPage(page, limit);
        List<BorrowVo> propertyVoList = borrowMapper.getReturn();
        map.put("data", propertyVoList);
        map.put("code", 0);
        long total = pageObj.getTotal();
        map.put("count", total);
        return map;
    }

    @Override
    public Map<String, Object> getHistory(Integer page, Integer limit) {
        /*创建Map集合用于存放数据，及所需的页码数*/
        Map<String, Object> map = new HashMap<>();
        Page<Object> pageObj = PageHelper.startPage(page, limit);
        List<BorrowVo> propertyVoList = borrowMapper.getHistory();
        System.out.println("----------------------------------------------"+propertyVoList);
        map.put("data", propertyVoList);
        map.put("code", 0);
        long total = pageObj.getTotal();
        map.put("count", total);
        return map;
    }
}
